﻿
using HBase;
using Net.http;
using System;
using System.Collections.Generic;


namespace hCommand.open
{
    [RoutePrefix("Task")]
    public class TaskServer : BaseApiController
    {
        ICoreTaskServer coreTaskServer = WMSDalFactray.getDal<ICoreTaskServer>();


        ///Task/getComplete
        [HttpGet, Route("getComplete")]
        public BaseResult getComplete()
        {
            return BaseResult.Ok(coreTaskServer.GetAll<CoreTask>("and  status=2"));
        }

        ///Task/getComplete
        [HttpGet, Route("updatePriority/{id}")]
        public BaseResult updatePriority(long id)
        {
            CoreTask ct = coreTaskServer.getById<CoreTask>(id);
            if (ct == null) {
                return BaseResult.Ok(id+"not find");
            }
            ct.priority = ct.priority + 1;
            coreTaskServer.updateNotNull(ct);
            var list= BaseTask.getTaskByCoreTaskId(ct.id);
            foreach (BaseTask bt in list) { 
                bt.priority= ct.priority;
            }
            return BaseResult.Ok(list.Count+"p"+ ct.priority);
        }

        [HttpGet, Route("GetCompleteParam/{id}")]
        public BaseResult GetCompleteParam(long id)
        {
            return BaseResult.Ok(MyCache.query<CoreTaskParam>((a) =>
            {
                return a.taskId == id;
            }));
        }
        [HttpGet, Route("finishTask/{stn}/{taskNo}")]
        public BaseResult finishTask(int taskNo, int stn)
        {
            Node node = Node.getConfig(stn);
            CoreTaskParam p1 = getCompleteParamByTaskNo(taskNo);
            if(p1 == null) {
                return BaseResult.Ok("任务完成");
            }
            if (node == null)
            {
                return BaseResult.Ok("站台不存在");
            }
            

            bool arived = PathFacade.isArived(stn, p1.des, null, 0);
            if (!arived)
            {
                return BaseResult.Ok("路径不可达" + stn + "=>" + p1.des);
            }
            
            int connt = coreTaskServer.updateParamAt(p1.id, stn);
            CoreTaskParam p = CoreTaskParam.getCoreTaskParam(p1.id);
            if (p.des == stn)
            {
                return BaseResult.Ok(connt == 1);
            }

            return BaseResult.Ok(connt == 1);
        }


        [HttpGet, Route("getComplete/{taskNo}")]
        public BaseResult getComplete(int taskNo)
        {
            CoreTaskParam p1 = getCompleteParamByTaskNo(taskNo);
            CoreTask complete = coreTaskServer.getById<CoreTask>(p1.taskId);
            return BaseResult.Ok(complete);
        }
        [HttpGet, Route("move/{src}/{des}")]
        public BaseResult move(int src, int des)
        {
            WMSBaseTransFacade.doWcsMoveTask( src, des);
            return BaseResult.Ok("任务生成");
        }

        [HttpGet, Route("DeleteCraneAndCarrierTask/{taskNo}")]
        public bool DeleteCraneAndCarrierTask(int taskNo)
        {

            CoreTaskParam p = MyCache.queryUnique<CoreTaskParam>((a) =>
            {
                return a.taskNo == taskNo;
            }, "CompleteParam 过多" + taskNo);
          
            String v= coreTaskServer.paramDelete( p.id); 
            if (String.IsNullOrEmpty(v))
            {
                PathFacade.removeComplete(p.taskId);
                return true;
            }
            return false;

        }
        static public CoreTaskParam getCompleteParamByTaskNo(int taskNo)
        {
            return MyCache.queryUnique<CoreTaskParam>((a) =>
            {
                return a.taskNo == taskNo ;
            }, "CompleteParam 过多" + taskNo);

        }
        [HttpGet, Route("getTaskByCompleteId/{deviceType}/{id}")]
        public BaseResult getTaskByCompleteId(string deviceType, long id)
        {
            Type t = PathFacade.getType(deviceType);
            return BaseResult.Ok(BaseTask.getTaskByCompleteId(t, id));
        }


        [HttpGet, Route("getAllTaskByCompleteId/{id}")]
        public BaseResult getAllTaskByCompleteId(long id)
        {
            List<Type> types = PathFacade.getAllTaskType();
            Dictionary<string, List<object>> dict = new Dictionary<string, List<object>>();
            foreach (Type t in types)
            {
                List<BaseTask> list = BaseTask.getTaskByCompleteId(t, id);
                string name = t.Name;
                List<object> res = new List<object>();
                foreach (BaseTask bt in list)
                {
                    res.Add(bt.Clone());
                }
                dict.Add(name, res);
            }
            List<CoreTaskParam> paramList = MyCache.query<CoreTaskParam>((a) =>
            {
                return a.taskId == id;
            });
            ;
            List<object> param1 = new List<object>();
            foreach (CoreTaskParam p in paramList)
            {
                param1.Add(p);
            }
            dict.Add("completeParam", param1);
            return BaseResult.Ok(dict);
        }





        [HttpGet, Route("t/{taskNo}")]
        public BaseResult t(int taskNo)
        {

            return BaseResult.Ok(MyCache.query<Estimate>(a =>
            {
                return a.taskNo == taskNo;
            }));
        }
    }
}
